﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using TTEntity;
namespace TTDataAccess
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsDAOrder
    {
        clsDAConnection con = new clsDAConnection();
        public DataTable GetById(int id)
        {
            try
            {
                string str = "select * from tblOrder where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetAll()
        {
            try
            {
                string str = "select * from tblOrder";
                SqlCommand com = new SqlCommand(str, con.getCon());
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void Insert(clsOrder obj)
        {
            try
            {
                string str = "insert into tblOrder(Detail,FullName,Email,Phone,Address,AccountId)" +
                    " values(@Detail,@FullName,@Email,@Phone,@Address,@AccountId)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@Detail", obj.Detail);
                com.Parameters.AddWithValue("@FullName", obj.FullName);
                com.Parameters.AddWithValue("@Email", obj.Email);
                com.Parameters.AddWithValue("@Phone", obj.Phone);
                com.Parameters.AddWithValue("@Address", obj.Address);
                com.Parameters.AddWithValue("@AccountId", obj.AccountId);

                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsOrder obj)
        {
            try
            {
                string str = "Update tblOrder set Detail=@Detail,FullName=@FullName,Email=@Email,"+
                    "Phone=@Phone,Address=@Address,AccountId=@AccountId where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@Detail", obj.Detail);
                com.Parameters.AddWithValue("@FullName", obj.FullName);
                com.Parameters.AddWithValue("@Email", obj.Email);
                com.Parameters.AddWithValue("@Phone", obj.Phone);
                com.Parameters.AddWithValue("@Address", obj.Address);
                com.Parameters.AddWithValue("@AccountId", obj.AccountId);

                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblOrder where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
